package com.pinyoumei.web.controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.pinyoumei.base.TaotaoResult;
import com.pinyoumei.pojo.TbPay;
import com.pinyoumei.utils.IDUtils;
import com.pinyoumei.utils.JsonUtils;
import com.pinyoumei.web.fegin.PayServiceFegin;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class PayController {
	
	@Autowired
	private PayServiceFegin payServiceFeign;
	
	@RequestMapping(value="/pay/generatePayToken",method=RequestMethod.POST)
	public String generatePayToken(@RequestParam("userId")Integer userId,@RequestParam("typeId")Integer typeId,Model model){
		log.info("接收参数userId:{},typeId:{}",userId,typeId);
		TbPay pay=new TbPay();
		pay.setUserid(userId);
		pay.setTypeid(typeId);
		pay.setCreated(new Date());
		pay.setUpdated(new Date());
		BigDecimal price=new BigDecimal("1000");
		pay.setPrice(price);
		pay.setState(0);
		Long orderId=IDUtils.generateId();
		pay.setOrderid(orderId+"");
		String result=payServiceFeign.createPayToken(pay);
		log.info("#############调用支付令牌接口返回结果:"+result+"##################");
		TaotaoResult taotao=JsonUtils.jsonToPojo(result, TaotaoResult.class);
		model.addAttribute("orderId", pay.getOrderid());
		model.addAttribute("payToken", (String)taotao.getData());
		return "pay/welcome";
		
	}
	
	@RequestMapping(value="/pay/getByToken",method=RequestMethod.POST)
	public String getByToken(@RequestParam("payToken")String payToken,Model model){
		log.info("接收参数payToken:{}",payToken);
		String result=payServiceFeign.getByToken(payToken);
		log.info("#############payToken查询返回结果:"+result+"##################");	
		TaotaoResult taotao=JsonUtils.jsonToPojo(result, TaotaoResult.class);
		model.addAttribute("data", (String)taotao.getData());
		return "pay/welcome";
		
	}
	
	/**
	 * 使用token进行支付
	 * @param payToken
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping(value="/pay/aliPay",method=RequestMethod.POST)
	public void aliPay(@RequestParam("payToken")String payToken,HttpServletResponse response) throws IOException{
		//告诉浏览器,服务器响应的是一个html页面
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		// 1.参数验证
		if (StringUtils.isEmpty(payToken)) {
			return;
		}
		// 2.调用支付服务接口 获取支付宝html元素
		String result=payServiceFeign.getByToken(payToken);
		TaotaoResult taotao=JsonUtils.jsonToPojo(result, TaotaoResult.class);
		if(taotao.getStatus()!=200){
			String msg=taotao.getMsg();
			writer.println(msg);
			writer.close();
			return;
		}
		// 3.返回可以执行的html元素给客户端
		String payHtml = (String)taotao.getData();
		log.info("####PayController###payHtml:{}",payHtml);
		//4. 页面上进行渲染
		writer.println(payHtml);
		writer.close();
	}

}
